/**
 * 
 * 深圳市店联电子商务
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TSaleactSummary;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundGray;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.SaleactSummaryModel;
import com.ruipengkj.service.TSaleactSummaryService;
import com.ruipengkj.view.base.BasePagePanel;
import com.ruipengkj.view.baseDataView.SaleactAddDialog;
import com.ruipengkj.view.baseDataView.SaleactUpdateDialog;
import com.ruipengkj.view.memberView.MenberDetailDialog;
import com.ruipengkj.view.memberView.UpdateUserPanel;

/**
 * 说明：
 * 优惠卷
 * @Author：朱攀
 * @Date：2012-07-12
 */
public class SelectCouponPanel extends BasePagePanel<TSaleactSummary> implements Runnable{

	private static final long serialVersionUID = 1L;
	//--------------------定义弹出框-----------------------------------
	private UpdateUserPanel updateUserPanelDialog;// 修改用户面板
	
	private SaleactAddDialog  saleactAddDialog;  //增加自定义优惠面板
	private  SaleactUpdateDialog saleactUpdateDialog;  //修改自定义优惠面板
	
	private JButton addButton = null;//增加按钮
	private JButton delButton =null;   // 删除按钮
	private JButton updateButton =null;   // 修改按钮

	private JTable table;
	//private MemberQueryCardModel memberQueryCardModel;
	private SaleactSummaryModel  saleactSummaryModel;
	private PagingComponent pagingComponent;//翻页组件
	private PageView<TSaleactSummary> pageView;//翻页主键
	private JScrollPane tableJsp;
	private TSaleactSummaryService  tSaleactSummaryService  ;	
	private MenberDetailDialog menberDetailDialog;//优惠卷详细窗体
	public SelectCouponPanel() {
		tSaleactSummaryService=BuildSpringApplication.getInstance().getTSaleactSummaryServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		
	}
	
	private void init() {
		pageView=new PageView<TSaleactSummary>();
		saleactSummaryModel=new SaleactSummaryModel();
		table=uiFactory.createRPJTable(saleactSummaryModel, null, this, 30, JLabel.CENTER);
		//table设置列宽度 AUTO_RESIZE_SUBSEQUENT_COLUMNS 填满父容器  JTable.AUTO_RESIZE_OFF 自适应
		table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		tableJsp=uiFactory.createJScrollPane(table);
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		
		//this.add(this.topPanel(), BorderLayout.NORTH);
		this.add(tableJsp, BorderLayout.CENTER);
		//this.add(pagingComponent, BorderLayout.SOUTH);
		JPanel gardPanel=uiFactory.createGridJPanel(1, 2);
		JPanel jp1=uiFactory.createFlowJPanel(FlowLayout.LEFT);
		addButton = new RPJButtonRoundBlue("添加(Ctrl+A)");
		updateButton = new RPJButtonRoundGray("修改(Ctrl+M)");
		delButton = new RPJButtonRoundOrager("删除(Ctrl+D)");
		jp1.add(new JLabel("   "));
		jp1.add(addButton);
		jp1.add(updateButton);
		jp1.add(delButton);
		gardPanel.add(jp1);
		gardPanel.add(pagingComponent);
		this.add(gardPanel,BorderLayout.SOUTH);
		
		//排序
		TableRowSorter<SaleactSummaryModel> tableRowSorter = new TableRowSorter<SaleactSummaryModel>(saleactSummaryModel);  
		table.setRowSorter(tableRowSorter);
	}
	
//	private JPanel topPanel(){
//		JPanel jp=new JPanel(new FlowLayout(FlowLayout.LEFT));
//		addButton = new JButton("增加");
//		delButton =new JButton("删除");
//		updateButton =new JButton("修改");
//		
//	
//		jp.add(addButton);
//		
//		jp.add(updateButton);
//		jp.add(delButton);
//		
//		//-----------------------------
//		jp.add(delButton); 
//		return jp;
//	}

	private void initListener(){
		addButton.addActionListener(this);
		addButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		delButton.addActionListener(this);
		delButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		updateButton.addActionListener(this);
		updateButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
	}
	@Override
	public void run() {
		this.termQuery(1);
	}
	//条件查询
	protected void termQuery(final int currentPage) {
		final TSaleactSummary tSaleactSummary=new TSaleactSummary();
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					//PageModel<SaleactSummaryModel> pageModel=tSaleactSummaryService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tSaleactSummary);
					
					PageModel<TSaleactSummary> pageModel=tSaleactSummaryService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),tSaleactSummary);
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						Util.clearTableRowSorter(table);
						saleactSummaryModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								Util.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	@Override
	protected PageView<TSaleactSummary> getPageView() {
		return pageView;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==addButton){
			//增加优惠卷
			addSalect();
		}
		
	else if(e.getSource()==delButton){ //---------------------------------------------------
			// 删除优惠卷
			this.deleteSaleact();
			
		}
		else if (e.getSource() == updateButton) {
			// 修改按钮
			modifySaleact();
		} 
	}
	
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			//if(e.getSource()==nameTF.getText()||e.getSource() == tel.getText()){
			if(e.getSource() == addButton){
				//增加自定义优惠卷
				addSalect();
			} else if (e.getSource() == table) { //--------------------------------------------------
				if (e.getKeyCode() == KeyEvent.VK_DELETE) {
					// 删除优惠卷
					deleteSaleact();
				}
			}
			else if (e.getSource() == updateButton) {
				// 修改按钮
				modifySaleact();
			} 
		}
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
    	if(e.getSource()==table&&e.getClickCount() == 2){
    		//表格
    		String id=table.getValueAt(table.getSelectedRow(), 0).toString();
    		try {
    			TSaleactSummary tSaleactSummary=tSaleactSummaryService.getByIdService(id);
				if(tSaleactSummary==null){
					JOptionPane.showMessageDialog(null, "当前信息不存在");
					return;
				}
				
				
				saleactUpdateDialog = new SaleactUpdateDialog(null, "修改自定义优惠", true);
				saleactUpdateDialog.showDialog(tSaleactSummary);
				if(saleactUpdateDialog.isSuccess==true){
					this.termQuery(1);
				}
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
    	}
    }
	//------------------------------------------------------------------------------------
	
	
	//增加优惠卷
	public void  addSalect(){
		try {
			
			saleactAddDialog = new SaleactAddDialog(null, "增加自定义优惠卷", true);
			saleactAddDialog.showDialog();
			if(saleactAddDialog.isSuccess==true){
				this.termQuery(1);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	
	// 删除选中的优惠卷
	private void deleteSaleact() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				if (Util.showConfirmDialog("是否要删除选择记录") == 0) {
					String sid = (String) table.getValueAt(row, 0);
					try {
						tSaleactSummaryService.deleteService(sid);
						JOptionPane.showMessageDialog(null, "删除成功");
						this.termQuery(1);
					} catch (Exception e1) {
						JOptionPane.showMessageDialog(null, "删除失败");
						e1.printStackTrace();
					}
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行删除");
				return;
			}
		}
	}
//----------------------------修改自定义优惠---------------------------
	

	private void modifySaleact() {
		if (table.getRowCount() != 0) {
			int row = table.getSelectedRow();
			if (row != -1) {
				String giftid = (String) table.getValueAt(row, 0);
				try {
					TSaleactSummary saleact = tSaleactSummaryService.getByIdService(giftid);
					saleactUpdateDialog = new SaleactUpdateDialog(null, "修改自定义优惠", true);
					saleactUpdateDialog.showDialog(saleact);
					if(saleactUpdateDialog.isSuccess==true){
						this.termQuery(1);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择行在进行修改");
				return;
			}
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	
}
